package com.mmy.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.MaterialBase;
import com.mmy.domain.oa.MaterialIn;
import com.mmy.domain.oa.VMaterialIn;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.MaterialBaseServiceImpl;
import com.mmy.service.oa.impl.MaterialInServiceImpl;

/**
 * 类的介绍：物料入库
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class MaterialInController {

	@Autowired
	MaterialInServiceImpl materialInServiceImpl;
	@Autowired
	MaterialBaseServiceImpl materialBaseServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-30
	 */
	@RequestMapping(value = "/materialin")
	public String ToMaterialIn(Model model) {
		try {
			List<VMaterialIn> lstMaterialIn = materialInServiceImpl.findAll();
			model.addAttribute("milist", lstMaterialIn);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialin";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/creatematerialin")
	public String ToCreateMaterialIn(Model model) {

		List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
		model.addAttribute("mblist", lstMaterialBase);
		return "OA/material/creatematerialin";
	}

	/**
	 * 函数功能说明： 新建物料入库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-07-30
	 */

	@RequestMapping(value = "/subcreatematerialin")
	public String CreateMaterialIn(MaterialIn materialIn, HttpSession session) throws Exception {
//		获取最大值
		Object oInID = materialInServiceImpl.getMaxInID();
		/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
		if (oInID == null) {
			materialIn.setInID(1);

		} else {
			materialIn.setInID(Integer.parseInt(oInID.toString()) + 1);
		}


		try {
			materialIn.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			materialIn.setCreator(tSysUser.getUserName());
			materialInServiceImpl.save(materialIn.getInID());
			materialInServiceImpl.save(materialIn);
			int iMaterialID = materialIn.getMaterialID();
			MaterialBase materialBase = materialBaseServiceImpl.findByMaterialID(iMaterialID);
			int iNumber = materialBase.getNumber() + materialIn.getNumber();
			materialBaseServiceImpl.updateNumberByMaterialID(iNumber, iMaterialID);
			return "redirect:materialin";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除物料入库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@PostMapping("/delmaterialin")
	@ResponseBody
	public String DelMaterialIn(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstInID = new ArrayList<>();

		for (String str : checklist) {
			lstInID.add(Integer.parseInt(str));
		}

		materialInServiceImpl.delMaterialIn(lstInID);

		return "OA/material/materialin";
	}

	/**
	 * 函数功能说明： 修改物料入库
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@RequestMapping(value = "/enterupdatematerialin")
	public String EnterUpdateMaterialIn(HttpServletRequest request, Model model) {

		int iInID = Integer.parseInt(request.getParameter("iInID"));

		MaterialIn materialIn = materialInServiceImpl.findByInID(iInID);
		model.addAttribute("materialIn", materialIn);
		List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
		model.addAttribute("mblist", lstMaterialBase);
		return "OA/material/updatematerialin";

	}

	@RequestMapping(value = "/updatematerialin")
	public String UpdateMaterialIn(MaterialIn materialIn, Model model, HttpSession session) throws Exception {

		try {
			/* 定义一个新的实体类对象 */
			MaterialIn dmaterialIn = materialInServiceImpl.findByInID(materialIn.getInID());
			int iMaterialID = dmaterialIn.getMaterialID();
			MaterialBase materialBase = materialBaseServiceImpl.findByMaterialID(iMaterialID);
			int iNumber = materialBase.getNumber() - dmaterialIn.getNumber() + materialIn.getNumber();
			dmaterialIn.setMaterialID(materialIn.getMaterialID());
			dmaterialIn.setNumber(materialIn.getNumber());
			dmaterialIn.setSumPrice(materialIn.getSumPrice());
			dmaterialIn.setMemo(materialIn.getMemo());
			dmaterialIn.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dmaterialIn.setReviser(tSysUser.getUserName());
			materialInServiceImpl.save(dmaterialIn);
			materialBaseServiceImpl.updateNumberByMaterialID(iNumber, iMaterialID);
			return "redirect:materialin";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询物料入库信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */
	@RequestMapping(value = "/searchmaterialin")
	public String FindMaterialIn(HttpServletRequest request, Model model) {

		String strMaterialName = request.getParameter("MaterialName");
		if (strMaterialName == "") {
			List<VMaterialIn> lstMaterialIn = materialInServiceImpl.findAll();
			model.addAttribute("milist", lstMaterialIn);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialin";
		} else {
			List<VMaterialIn> lstMaterialIn = materialInServiceImpl.findByMaterialName(strMaterialName);
			model.addAttribute("milist", lstMaterialIn);
			List<MaterialBase> lstMaterialBase = materialBaseServiceImpl.findAll();
			model.addAttribute("mblist", lstMaterialBase);
			return "OA/material/materialin";
		}

	}

}
